import { Context, controller,inject, provide, post } from 'midway';
import { Cart } from '../../entity/Cart';



@provide()
@controller('/cart')
export class CartController {

  @inject()
  ctx: Context;

  @post('/add')
  async addShopCart(){
    const body=this.ctx.request.body
    console.log(body);
   
    let cart=await Cart.findOne({gcode:body.gcode,userid:body.userid})
    if(cart){
      cart.count+=parseInt(body.count)
    }else{
      cart=Cart.create(body)  
    }
    await cart.save()
    this.ctx.body={status:'OK'}
  }
  @post('/delete')
  async delete(){
    const body=this.ctx.request.body
    console.log(body);
   
    let cart=await Cart.findOne({gcode:body.gcode,userid:body.userid})
    await cart.remove()
    this.ctx.body={status:'OK'}
  }

}
